/*
 * Copyright 2013 meichenhui
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.googlecode.zet.tool.pluginanalyzer;

import java.util.Collections;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.eclipse.osgi.service.resolver.BaseDescription;
import org.eclipse.osgi.service.resolver.BundleDescription;
import org.eclipse.osgi.service.resolver.BundleSpecification;
import org.eclipse.osgi.service.resolver.VersionRange;

/**
 * 依赖插件描述<br/>
 * 没有完全实现{@link BundleSpecification}接口,因为用不上那么多属性.<br/>
 * 调用没有实现的接口会抛出{@link UnsupportedOperationException}异常.
 * @author meichenhui
 *
 */
public class RequireBundleDescription implements BundleSpecification {
    
    private String name;
    
    private VersionRange versionRange;
    
    public RequireBundleDescription(String name,VersionRange versionRange) {
        setName(name);
        setVersionRange(versionRange == null ? VersionRange.emptyRange : versionRange);
    }
    
    protected void setName(String name){
        this.name = name;
    }

    @Override
    public String getName() {
        return name;
    }
    
    protected void setVersionRange(VersionRange versionRange){
        this.versionRange = versionRange;
    }

    @Override
    public VersionRange getVersionRange() {
        return versionRange;
    }
    
    @Override
    public boolean equals(Object obj) {
        if (obj instanceof RequireBundleDescription){
            RequireBundleDescription input = (RequireBundleDescription) obj;
            if (!StringUtils.equals(getName(), input.getName())){
                return false;
            }
            if (getVersionRange() == null ? input.getVersionRange() == null : getVersionRange().equals(input.getVersionRange())){
                return true;
            }
        }
        return super.equals(obj);
    }
    
    @Override
    public int hashCode() {
        return HashCodeBuilder.reflectionHashCode(this, Collections.<String> emptyList());
    }
    
    @Override
    public boolean isSatisfiedBy(BaseDescription supplier) {
        if (!(supplier instanceof BundleDescription))
            return false;
        BundleDescription candidate = (BundleDescription) supplier;
        if (getName() != null && getName().equals(candidate.getSymbolicName()) && (getVersionRange() == null || getVersionRange().isIncluded(candidate.getVersion())))
            return true;
        return false;
    }
    
    public String toString() {
        return "Require-Bundle: " + getName() + "; bundle-version=\"" + getVersionRange() + "\"";
    }
    
    /**
     * 没有被实现,会抛出{@link UnsupportedOerationException}
     * @deprecated
     */
    @Override
    public BundleDescription getBundle() {
        throw new UnsupportedOperationException();
    }
    
    /**
     * 没有被实现,会抛出{@link UnsupportedOerationException}
     * @deprecated
     */
    @Override
    public boolean isResolved() {
        throw new UnsupportedOperationException();
    }
    
    /**
     * 没有被实现,会抛出{@link UnsupportedOerationException}
     * @deprecated
     */
    @Override
    public BaseDescription getSupplier() {
        throw new UnsupportedOperationException();
    }
    
    /**
     * 没有被实现,会抛出{@link UnsupportedOerationException}
     * @deprecated
     */
    @Override
    public boolean isExported() {
        throw new UnsupportedOperationException();
    }
    
    /**
     * 没有被实现,会抛出{@link UnsupportedOerationException}
     * @deprecated
     */
    @Override
    public boolean isOptional() {
        throw new UnsupportedOperationException();
    }

}
